/*
 * Created by SharpDevelop.
 * User: dvetters
 * Date: 8/4/2010
 * Time: 9:54 AM
 */
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Windows.Forms;
using System.Collections.Generic;
using Declarations;
using Implementation;

namespace Echium2
{
	/// <summary>
	/// Description of SplashForm.
	/// </summary>
	public partial class SplashForm : Form
	{
		Thread loadingThread;
		Thread textThread;
		
		public SplashForm()
		{
			InitializeComponent();
			textThread = new Thread(new ThreadStart(cycleLoadingText));
			loadingThread = new Thread(new ThreadStart(loadVlcClient));
		}
		
		private void loadVlcClient()
		{
			MediaGlobals.factory = new MediaPlayerFactory();
			MediaGlobals.player = MediaGlobals.factory.CreatePlayer<IAudioPlayer>();
			MediaGlobals.MainLibrary = new EchiumLibrary();
			if (System.IO.File.Exists(Application.StartupPath+"\\Library.xml"))
			{
				MediaGlobals.MainLibrary.LoadLibrary(Application.StartupPath+"\\Library.xml");
			}
			LoadingDone(this, new EventArgs());
		}
		
		private void cycleLoadingText()
		{
			while(true)
			{
				setLoadingLabelText(this, new EventArgs<string>("Loading, please wait.."));
				Thread.Sleep(1000);
				setLoadingLabelText(this, new EventArgs<string>("Loading, please wait..."));
				Thread.Sleep(1000);
				setLoadingLabelText(this, new EventArgs<string>("Loading, please wait.."));
				Thread.Sleep(1000);
				setLoadingLabelText(this, new EventArgs<string>("Loading, please wait."));
				Thread.Sleep(1000);
			}
		}
		
		private void setLoadingLabelText(object sender, EventArgs<string> e)
		{
			if (InvokeRequired)
			{
				this.BeginInvoke(new EventHandler<EventArgs<string>>(setLoadingLabelText), sender, e);
			}
			else
			{
				this.loadingLabel.Text = e.Arg;
			}
		}
		
		private void LoadingDone(object sender, EventArgs e)
		{
			if (InvokeRequired)
			{
				this.BeginInvoke(new EventHandler(LoadingDone), sender, e);
			}
			else
			{
				textThread.Abort();
				this.Close();
			}
		}
		
		private void SplashFormShown(object sender, EventArgs e)
		{
			textThread.Start();
			loadingThread.Start();
		}
		
		protected override void OnPaintBackground(PaintEventArgs e)
		{
			Brush Gradient_Brush = new LinearGradientBrush(this.DisplayRectangle, Color.FloralWhite, Color.LightBlue, LinearGradientMode.Vertical);
			e.Graphics.FillRectangle(Gradient_Brush, this.DisplayRectangle);
		}
	}
	
	public class EventArgs<T> : EventArgs
	{
		public T Arg
		{
			get; private set;
		}
		
		public EventArgs(T arg)
		{
			this.Arg = arg;
		}
	}
}
